import cloudbase from '@cloudbase/js-sdk'
import ElementUI from 'element-ui';

// 初始化云环境
const app = cloudbase.init({
	env: 'web-cloud0713-6g519i65f479e638',
})

// 默认 匿名登录
// const auth = app.auth();
// async function login(){
//     await auth.anonymousAuthProvider().signIn()
//     // // 匿名登录成功检测登录状态isAnonymous字段为true
//     const loginState = await auth.getLoginState();
//     const isLogin = loginState.isAnonymousAuth
//     console.log(isLogin); // true
// }
// login()

// const ishasLoginState = app.auth().hasLoginState()
// console.log(ishasLoginState)
export function get(dbName, params) {
    return new Promise((resolve, reject) => {
        //  显示等待框
        const loadings = ElementUI.Loading.service({
            lock: true,
            text: '加载中，请稍候。。。',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });
        app.callFunction({name:dbName,data:params}).then(res => {
            if(res.result){
                if(res.result.type=='error'){
                    ElementUI.Message.error(res.result.msg)
                }else{
                    resolve(res.result);
                }
            }
            loadings.close();
        }).catch(function (error) {
            alert(error)
            loadings.close();
            reject(error);
        });
    });
}
export default get;